package io.renren.modules.demo.algorithm;

import java.util.ArrayList;

public class Population
{
    private ArrayList<Chromosome> population;  //种群中有许多个体

    private double fitnessSum;  //所有个体的适应度总和

    public ArrayList<Chromosome> getPopulation()
    {
        return population;
    }

    public void setPopulation(ArrayList<Chromosome> population)
    {
        this.population = new ArrayList<>(population);
    }

    public double getFitnessSum()
    {
        return fitnessSum;
    }

    public void setFitnessSum(double fitnessSum)
    {
        this.fitnessSum = fitnessSum;
    }

    public Population()
    {

    }

    public Population(ArrayList<Chromosome> population)
    {
        this.population = new ArrayList<>(population);
        CalculateFitnessSum();
    }

    public void CalculateFitnessSum()
    {
        double fitnessSum = 0;
        for (int i = 0; i < population.size(); i++)
        {
            population.get(i).CalculateFitness();
            fitnessSum += population.get(i).getFitness();
        }
        this.fitnessSum = fitnessSum;
    }

    public void Initialize(int populationNumber)
    {
        population = new ArrayList<>();
        for (int i = 0; i < populationNumber; i++)
        {
            Chromosome chromosome = new Chromosome();
            chromosome.Initialize();
            if (chromosome.getChromosome() != null)
                population.add(chromosome);
            else
                i--;
        }
        CalculateFitnessSum();
    }
}
